home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug106 / strfun.asm < prev    next >
Assembly Source File  |  1984-06-14  |  5KB  |  192 lines

  1. ;                strfun.asm
  2. ;
  3. ;    Copyright (C) 1980, M J Maney
  4. ;
  5. ;    First created 8/25/80
  6. ;    Last revised 8/30/80 19:15
  7. ;
  8. ;    Herein are the basic string functions that C people like to use,
  9. ;    coded in assembler to make them as fast as can be.
  10. ;
  11. ;    NOTE that I have used .NE. in the comments in place of the usual C
  12. ;    symbol, because MAC sometimes objects to such usage.
  13. ;
  14.     maclib    crl
  15. ;
  16. ;
  17. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  18. ;
  19. ;                string functions
  20. ;
  21. ;    strlen        strcmp        strcpy        strcat
  22. ;
  23. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  24. ;
  25. ;
  26. ;    strlen(str)
  27. ;    char *str;
  28. ;
  29. ;    Returns the length of the string pointed to by str. It is assumed
  30. ;    that the string is terminated by '\0', and the terminator is NOT
  31. ;    counted as a character, so the storage length of a string s is
  32. ;    strlen(s) + 1.
  33. ;
  34.     PROC    STRLEN
  35.     lxi    d,0
  36.     lhld    ARG1        ;str to HL
  37.     mvi    a,0
  38. strlen1    cmp    m
  39.     BZ    strlen2        ;for (i=0; *str++ .NE. EOS; i++) ;
  40.     inx    h
  41.     inx    d
  42.     BRA    strlen1
  43. ;
  44. strlen2    xchg            ;return i;
  45.     ret
  46.     PEND    STRLEN
  47. ;
  48. ;
  49. ;    strcmp(sx,sy)
  50. ;    char *sx,*sy;
  51. ;
  52. ;    Returns 1, 0, or -1, depending upon the lexicographical relation
  53. ;    of the strings pointed to by sx and sy. The ordering is essentially
  54. ;    that which would be used if the strings were words being alphebetized
  55. ;    except that upper case and lower case are distinct, and digits and
  56. ;    punctuation are treated as significant characters. The relation
  57. ;    between any two characters is simply the relation between the bit
  58. ;    patterns that are used to represent them, treated as unsigned
  59. ;    integers. It is also assumed that the string terminator, '\0', has
  60. ;    a smaller value than any other character (in fact, its zero).
  61. ;
  62. ;    1 if sx > sy, 0 if sx == sy, or -1 if sx < sy
  63. ;
  64.     PROC    STRCMP
  65.     lhld    ARG1
  66.     xchg            ;sx to DE
  67.     lhld    ARG2        ;sy to HL
  68. ;
  69. strcmp1    ldax    d
  70.     cmp    m        ;while (*sy == (c = *sx)) {
  71.     BNZ    strcmp2
  72.     inx    h        ;    sy++;
  73.     inx    d        ;    sx++;
  74.     ora    a
  75.     BNZ    strcmp1        ;    if (c == EOS)
  76.     lxi    h,0        ;        return 0;
  77.     ret            ;    }
  78. strcmp2    lxi    h,1        ;if (*sx > *sy)
  79.     rnc            ;    return 1;
  80.     dcx    h        ;else
  81.     dcx    h
  82.     ret            ;    return -1;
  83.     PEND    STRCMP
  84. ;
  85. ;
  86. ;    strcpy(des,src)
  87. ;    char *des,*src;
  88. ;
  89. ;    Copies string pointed to by src into memory starting at des. No
  90. ;    provision is made for limiting the amount of stuff copied: it is
  91. ;    the user's responsibility to insure that the string being copied
  92. ;    will fit where it is being copied to.
  93. ;
  94.     PROC    STRCPY
  95.     lhld    ARG1
  96.     xchg            ;des to DE
  97.     lhld    ARG2        ;src to HL
  98. strcpy1    mov    a,m        ;do {
  99.     inx    h
  100.     stax    d
  101.     inx    d        ;    *des++ = c = *src++;
  102.     ora    a
  103.     BNZ    strcpy1        ;    }while (c .NE. EOS);
  104.     ret            ;return;
  105.     PEND    STRCPY
  106. ;
  107. ;
  108. ;    strcat(des,src)
  109. ;    char *des,*src;
  110. ;
  111. ;    Copies the string pointed to by src onto the tail end of the string
  112. ;    pointed to by des. This is equivalent to
  113. ;
  114. ;        strcpy(des+strlen(des),src)
  115. ;
  116. ;    and there is an identical lack of testing for overrun during the
  117. ;    copying process.
  118. ;
  119.     PROC    STRCAT
  120.     lhld    ARG2
  121.     xchg            ;src to DE
  122.     lhld    ARG1        ;des to HL
  123.     sub    a
  124. strcat1    cmp    m        ;while (*des++ .NE. EOS)
  125.     inx    h        ;    ;
  126.     BNZ    strcat1
  127.     dcx    h        ;des--;
  128. strcat2    ldax    d        ;do {
  129.     inx    d
  130.     mov    m,a
  131.     inx    h        ;    *des++ = c = *src++;
  132.     ora    a
  133.     BNZ    strcat2        ;    }while (c .NE. EOS);
  134.     ret            ;return;
  135.     PEND    STRCAT
  136. ;
  137. ;
  138.     end
  139. P;
  140. }
  141.  
  142. insert(s,is,fpos)
  143. int fpos;
  144. char *s, *is;
  145. {
  146.     char *tp; tp = SBP;
  147.  
  148.     while (--fpos != 0) *tp++ = *s++;
  149.     while (*tp++ = *is++); --tp;
  150.     while (*tp++ = *s++); return SBP;
  151. }
  152.     
  153. left(s,n)
  154. int n;
  155. char *s;
  156. {
  157.     char *tp; tp = SBP;
  158.  
  159.     while (n-/*                stdio.src
  160.  
  161.     Copyright (C) 1980, M J Maney
  162.  
  163.     First created    3/15/80
  164.     Last revised    4/19/80 1:45
  165.  
  166.     This file defines some useful functions that are designed to help
  167.     make BDS C programs running under CP/M pretend that they're
  168.     running in a nicer environment (like UNIX, ha-ha). It makes it
  169.     easier to setup and use STDIN and STDOUT directed IO, and
  170.     although you can't just assume that they're there as you can
  171.     with UNIX C, at least you don't have to put in code to parse
  172.     the command line into each program you write: just use the
  173.     functions here.
  174. */
  175.  
  176.  
  177. #include "csym.lib"
  178. #include "stdio.lib"
  179.  
  180. #define ESCAPE '\\'
  181.  
  182.  
  183. /* stdopen parses the command line arguments looking for arguments that
  184.     begin with "<" or ">", which are respectively taken to be the
  185.     filenames for STDIN and STDOUT. The defaults are to use the
  186.     console for both STDIN and STDOUT. Any argument beginning with
  187.     "<" or ">" is effectively removed from the argument list (by
  188.     moving pointers in argv[] and decreasing argc)...if your program
  189.     wants to get an argument beginning with "<" or ">", you must
  190.     type it as "\<" or "\>", and stdopen will replace the escape
  191.     sequence in the strings, but will not "use" those arguments
  192.     for IO assignment. If you want to pass arguments beg